package com.ssy.dao;

import com.ssy.common.PageInfo;
import com.ssy.common.Result;
import com.ssy.entity.Course;
import com.ssy.entity.Curriculum;
import com.ssy.vo.CurriculumVo;

import java.util.HashMap;
import java.util.List;

/**
 * curriculumDao
 *
 * @author ssy
 * @date 2020/11/19
 **/
public class CurriculumDao extends BaseDao {

    public PageInfo<CurriculumVo> selectPage(HashMap<String, Object> params, Integer pageNo, Integer limitNo) {
        String sql = "SELECT cu.cum_id AS cumId,co.course_id AS courseId,cu.number AS number,c.c_id AS cId,\n" +
                "cu.work_id AS workId,c.cname AS className,co.name AS courseName,teacher AS teacher,\n" +
                "StartTime AS startTime,address AS address\n" +
                "FROM curriculum cu INNER JOIN class c ON cu.classId=c.c_id INNER JOIN course co ON cu.course_id=co.course_id where";
        if (params.containsKey("workId")) {
            sql = sql + " cu.work_id = " + params.get("workId") + "  and";
        }
        if (params.containsKey("number")) {
            sql = sql + " cu.number like '%" + params.get("number") + "%'  and";
        }
        if (params.containsKey("teacher")) {
            sql = sql + " teacher like '%" + params.get("teacher") + "%'  and";
        }
        if (params.containsKey("classId")) {
            sql = sql + " cu.classId = " + params.get("classId") + "  and";
        }
        if (sql.endsWith("and")) {
            sql = sql.substring(0, sql.lastIndexOf("and"));
        }
        if (sql.endsWith("where")) {
            sql = sql.substring(0, sql.lastIndexOf("where"));
        }
        return this.selectPage(sql, CurriculumVo.class, pageNo, limitNo);

    }

    public boolean insertCurriculm(int workId1, int number1, int courseId1, int classId1, String teacher, String address, String startTime) {
        String sql = "INSERT INTO curriculum(work_id,number,classId,course_id,teacher,StartTime,address) VALUE(?,?,?,?,?,?,?)";
        int curriculmId = this.insert(sql, workId1, number1, classId1, courseId1, teacher, startTime, address);
        return curriculmId > 0 ? true : false;
    }

    public Result queryAllCourseList() {
        String sql = "SELECT course_id AS courseId,name AS name FROM course GROUP BY course_id";
        List<Course> courseLsit = this.selectList(sql, Course.class);
        return new Result(courseLsit);
    }

    public boolean updateCurriculm(int workId1, int number1, int courseId1, int classId1, String teacher, String address, String startTime, int cumId1) {
        String sql = "UPDATE curriculum SET work_id=?,number=?,classId=?,course_id=?,teacher=?,StartTime=?,address=? WHERE cum_id=?";
        return this.update(sql, workId1, number1, classId1, courseId1, teacher, startTime, address, cumId1);
    }

    public boolean deleteOneCurriculum(int id) {
        String sql = "DELETE FROM curriculum WHERE cum_id=?";
        return this.update(sql, id);
    }

    public boolean batchDeleteCurriculum(String[] ids) {
        String sql = "DELETE FROM curriculum WHERE cum_id=?";
        return this.batchDelete(sql, ids);
    }

    public int queryCurriculumByClassIdAndCourseId(int classNameId, int courseNameId) {
        String sql = "SELECT cum_id AS cumId  FROM curriculum WHERE classId=? AND course_id=?";
        Curriculum curriculum = this.selectOne(sql, Curriculum.class, classNameId, courseNameId);
        if (curriculum == null) {
            return -1;
        }
        return curriculum.getCumId();
    }
}
